<?php
/*

//type 1 漂流 2 溯溪 3 套票
//channel 1 手机端 2 微信和官网 3 团购
sfzcode=510215196411020464&type=03&channel=1
*/


namespace APP;

class Control{

	private $ctr; //1 发出 2 查询后更新
	private $type_name;

	public function __construct($ctr=1,$type){
		$this->ctr=$ctr;
		if($type == '01'){
			$this->type_name='漂流';
		}elseif ($type == '02') {
			$this->type_name='溯溪';
		}elseif($type=='03'){
			$this->type_name='套票';
		}		
	}

	public function selectStr($sfzcode,$channel){
		if( $channel==1 || $channel==2){
			$sheet = ($channel==1) ? 'tim_piao_ding' : 'tim_baopiao_ding';
			$strsql = "select * from {$sheet} where sfzcode='{$sfzcode}' and zhifu=1 and flag=1 and type='{$this->type_name}';";			
			return $strsql; 	
		}else{
			$strsql = "select * from tim_tuan where sfzcode='{$sfzcode}' and flag='0' and type='{$this->type_name}';";
			return $strsql;
		}
	}

	public function onQuery($query){
		$query_arr['id']=null;
		$query_arr['num']=null;
		$query_arr['yuyue']=null;

		foreach ($query as $key => $value) {
			$query_arr['id'] =$query_arr['id'].','.$value['id'];
			$query_arr['num'] += !empty($value['num']) ? $value['num'] : $value['num1'];
			$query_arr['yuyue'] = !empty($value['dao_date']) ? $value['dao_date'] : $value['yuyue_date'];
		}
		$query_arr['id']=trim($query_arr['id'],',');
		return $query_arr; 
	}

	public function updateSQL($id=null,$useId=null,$sfzcode=null,$channel=null){
		$id_arr=explode(',',$id);
		$nowDate=date("Y-m-d H:i:s");
		if($channel == 1){
			$upsql_str="update tim_piao_ding set flag=2,zhifu=1,xiaofei_date='{$nowDate}',xiaofei_confirm='{$useId}' where ";
		}else if($channel == 2){
			$upsql_str="update tim_baopiao_ding set flag=2,zhifu=1,xiaofei_date='{$nowDate}',xiaofei_confirm='{$useId}' where ";
		}else if($channel == 3){
			$table = "tim_tuan";
			$upsql_str="update tim_tuan set flag=1,end_date='{$nowDate}',end_confirm='{$useId}' where ";
		}
		foreach ($id_arr as $key => $value) {
			$sql_str="select * from {$table} where id={$value} limit 1";
			$db= \APP\Factory::createObj("APP\db\Db");
			$query=$db->query($sql_str)->fetch_array();	
			if($query){
				$sql_str=$upsql_str." id={$value} limit 1";
				$query=$db->query($sql_str)->upCheck();
				if($query){
					echo 'ok';
				}else{
					echo 'no';
				}	
			}else{
			}
		}
	}

	public function createXml($xml_arr=null){
		if(!$xml_arr){
		 	$xml_arr['type']=0;
		 	$xml_arr['id']=0;
		 	$xml_arr['num']=0;
		 	$xml_arr['channel']=0;
		 	$xml_arr['sfzcode']=0;
		 	$xml_arr['yuyue']=0;
		}

$xml_str=<<<out

<root>
<ticket>
<id>{$xml_arr['id']}</id>
<sfzcode>{$xml_arr['sfzcode']}</sfzcode>
<yuyue>{$xml_arr['yuyue']}</yuyue>
<num>{$xml_arr['num']}</num>
<type>{$xml_arr['type']}</type>
<channel>{$xml_arr['channel']}</channel>
</ticket>
</root>
out;
		echo $xml_str;
		die();		
	}
}
?>